Start the Julia interpreter and use it as a calculator.
Practice using the Julia interpreter as a calculator:
Write a function named right_justify
that takes a string named s
as a parameter and prints the string with enough leading spaces so that the last letter of the string is in column 70 of the display.
right_justify('monty')
monty
Hint: Use string concatenation and repetition. Also, Julia provides a built-in function called length
that returns the length of a string, so the value of length("monty")
is 5.
A function object is a value you can assign to a variable or pass as an argument. For example, do_twice
is a function that takes a function object as an argument and calls it twice:
function do_twice(f)
f()
f()
end
Here’s an example that uses do_twice
to call a function named print_spam
twice.
function print_spam()
println("spam")
end
do_twice(print_spam)
do_twice
so that it takes two arguments, a function object and a value, and calls the function twice, passing the value as an argument.print_twice
from the lectures to your notebook.do_twice
to call print_twice
twice, passing "spam"
as an argument.do_four
that takes a function object and a value and calls the function four times, passing the value as a parameter. There should be only two statements in the body of this function, not four.Note: This exercise should be done using only the statements and other features we have learned so far. Write a function that draws a grid like the following:
+ - - - - + - - - - +
| | |
| | |
| | |
| | |
+ - - - - + - - - - +
| | |
| | |
| | |
| | |
+ - - - - + - - - - +
Hint: to print more than one value on a line, you can print a comma-separated sequence of values:
println("+ ", "- ")
println
advances to the next line, but print
doesn't:
print("+ ")
println("- ")
The output of these statements is "+ -"
on the same line. The output from the next print statement would begin on the next line.
Write a function that draws a similar grid with four rows and four columns.
circle(🐢, radius)
. You can do the arithmetic by hand or add print statements to the code.arc
in the lectures is not very accurate because the linear approximation of the circle is always outside the true circle. As a result, the Turtle ends up a few pixels away from the correct destination. My solution shows a way to reduce the effect of this error. Read the code and see if it makes sense to you. If you draw a diagram, you might see how it works:""" Draws an arc with the given radius and angle.
t: Turtle
r: radius
angle: angle subtended by the arc, in degrees
"""
function arc(t, r, angle)
arc_length = 2 * π * r * abs(angle) / 360
n = trunc(arc_length / 4) + 3
step_length = arc_length / n
step_angle = float(angle) / n
# making a slight left turn before starting reduces
# the error caused by the linear approximation of the arc
Turn(t, -step_angle/2)
polyline(t, n, step_length, step_angle)
Turn(t, step_angle/2)
end
In [33]:
using TikzPictures
TikzPicture(L"""
\draw (0,0) arc (0:-60:2) arc (180:120:2);
\draw (0,0) arc (60:0:2) arc (240:180:2);
\draw (0,0) arc (120:60:2) arc (300:240:2);
\draw (0,0) arc (180:120:2) arc (360:300:2);
\draw (0,0) arc (240:180:2) arc (60:0:2);
\draw (0,0) arc (300:240:2) arc (120:60:2);
\draw (5,0) arc (0:-60:2) arc (180:120:2);
\draw (5,0) arc (30:-30:2) arc (210:150:2);
\draw (5,0) arc (60:0:2) arc (240:180:2);
\draw (5,0) arc (90:30:2) arc (270:210:2);
\draw (5,0) arc (120:60:2) arc (300:240:2);
\draw (5,0) arc (150:90:2) arc (330:270:2);
\draw (5,0) arc (180:120:2) arc (360:300:2);
\draw (5,0) arc (210:150:2) arc (30:-30:2);
\draw (5,0) arc (240:180:2) arc (60:0:2);
\draw (5,0) arc (270:210:2) arc (90:30:2);
\draw (5,0) arc (300:240:2) arc (120:60:2);
\draw (5,0) arc (330:270:2) arc (150:90:2);
"""; options="very thick, scale=3, transform shape", preamble="""
\\usepackage{newtxmath}
\\renewcommand{\\familydefault}{\\sfdefault}
""")
Out[33]:
In [68]:
using TikzPictures
TikzPicture(L"""
\draw [turtle={home,right=36,forward,left=126,forward=2.351141cm,left=126,forward,left=144}];
\draw [turtle={home,right=72,right=36,forward,left=126,forward=2.351141cm,left=126,forward,left=144}];
\draw [turtle={home,right=144,right=36,forward,left=126,forward=2.351141cm,left=126,forward,left=144}];
\draw [turtle={home,right=216,right=36,forward,left=126,forward=2.351141cm,left=126,forward,left=144}];
\draw [turtle={home,right=288,right=36,forward,left=126,forward=2.351141cm,left=126,forward,left=144}];
\begin{scope}[xshift=6cm]
\draw [turtle={home,right=22.5,forward,left=112.5,forward=1.530734cm,left=112.5,forward}];
\draw [turtle={home,right=45,right=22.5,forward,left=112.5,forward=1.530734cm,left=112.5,forward}];
\draw [turtle={home,right=90,right=22.5,forward,left=112.5,forward=1.530734cm,left=112.5,forward}];
\draw [turtle={home,right=135,right=22.5,forward,left=112.5,forward=1.530734cm,left=112.5,forward}];
\draw [turtle={home,right=180,right=22.5,forward,left=112.5,forward=1.530734cm,left=112.5,forward}];
\draw [turtle={home,right=225,right=22.5,forward,left=112.5,forward=1.530734cm,left=112.5,forward}];
\draw [turtle={home,right=270,right=22.5,forward,left=112.5,forward=1.530734cm,left=112.5,forward}];
\draw [turtle={home,right=315,right=22.5,forward,left=112.5,forward=1.530734cm,left=112.5,forward}];
\end{scope}
"""; options="very thick, scale=2, transform shape, turtle/distance=2cm", preamble="""
\\usepackage{newtxmath}
\\renewcommand{\\familydefault}{\\sfdefault}
\\usetikzlibrary{turtle}
""")
Out[68]:
Read about spirals at http://en.wikipedia.org/wiki/Spiral; then write a program that draws an Archimedian spiral (or one of the other kinds).